package rabbit.topic;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/7/31 14:56
 */


public class HrConsumer {
    private static final String EXCHANGE_NAME = "hiddencallbusinessexchange";// "outerhostexchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory consumerFactory = new ConnectionFactory();
        consumerFactory.setHost("39.106.17.245");
        consumerFactory.setVirtualHost("OuterHost");
        consumerFactory.setUsername("root");
        consumerFactory.setPassword("root");
        Connection consumerConnection = consumerFactory.newConnection();
        Channel consumerChannel = consumerConnection.createChannel();
        String queueName = "q.hrlong";
        String topic = "hrlong";


        ConnectionFactory prodFactory = new ConnectionFactory();
        prodFactory.setHost("39.106.144.161");
        prodFactory.setVirtualHost("OuterHost");
        prodFactory.setUsername("root");
        prodFactory.setPassword("root");
        Connection prodConnection = prodFactory.newConnection();
        Channel prodChannel = prodConnection.createChannel();

        //prodChannel.getConnection().
        prodChannel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true);


        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(consumerChannel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
                prodChannel.basicPublish(EXCHANGE_NAME, topic, null, body);
            }
        };
        consumerChannel.basicConsume(queueName, true, consumer);
    }
}